package com.suneee.logistic.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suneee.context.page.Page;
import com.suneee.core.error.Assert;
import com.suneee.core.error.ErrorUtils;
import com.suneee.logistic.dao.LogisticDao;
import com.suneee.logistic.entity.Logistic;
import com.suneee.logistic.entity.LogisticDTO;
import com.suneee.logistic.service.LogisticService;
import com.suneee.resources.ProductError;

@Service("api-logistic")
public class LogisticServiceImpl implements LogisticService {

	@Autowired
	private LogisticDao logisticDao;

	@Override
	public void addLogistic(Logistic logistic) {
		Assert.notNull(logistic.getCompanyCode(), "物流编码");
		Assert.notNull(logistic.getCompanyName(), "物流公司名称");
		if (logisticDao.existsName(logistic.getCompanyName())) {
			throw ErrorUtils.wrap(ProductError.IOGISTIC_NAME_DUPLICATE);
		}
		if (logisticDao.existsId(logistic.getCompanyCode())) {
			throw ErrorUtils.wrap(ProductError.IOGISTIC_COMPANYCODE_DUPLICATE);
		}
		
		logisticDao.insert(logistic);
	}
	
	@Override
	public void addLogisticList(List<Logistic> logisList) {
		logisticDao.insert(logisList);
	}

	@Override
	public void updateLogistic(Logistic logistic) {
		Assert.notNull(logistic.getCompanyCode(), "物流编码");
		Assert.notNull(logistic.getCompanyName(), "物流名称");
		Logistic old = this.getLogistic(logistic.getCompanyCode());
		if (!old.getCompanyName().equals(logistic.getCompanyName())
				&& logisticDao.existsName(logistic.getCompanyName())) {
			throw ErrorUtils.wrap(ProductError.IOGISTIC_NAME_DUPLICATE);
		}
		logisticDao.update(logistic);
	}

	@Override
	public void deleteLogistic(String companyCode) {
		Assert.notNull(companyCode, "物流编码");
		if (!logisticDao.existsId(companyCode)) {
			throw ErrorUtils.wrap(ProductError.IOGISTIC_DUPLICATE);
		}
		logisticDao.delete(companyCode);

	}

	@Override
	public Logistic getLogistic(String companyCode) {
		Assert.notNull(companyCode, "物流编码");
		if (logisticDao.existsId(companyCode)) {
			return logisticDao.select(companyCode);
		} else {
			throw ErrorUtils.wrap(ProductError.IOGISTIC_DUPLICATE);
		}
	}

	
	@Override
	public List<Logistic> listLogisticEn(LogisticDTO dto) {
		if(dto == null)
			dto = new LogisticDTO();
		return logisticDao.listLogisticEn(dto);
	}

	@Override
	public Page<Logistic> listLogistic(LogisticDTO dto) {
		if(dto == null)
			dto = new LogisticDTO();
		return logisticDao.listLogistic(dto);
	}
	
	

}
